﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

namespace PaulBeck.Timesheet.WebParts.Admin.EditProjectResources
{
    public partial class EditProjectResourcesUserControl : UserControl
    {

        public int ProjectResourceId
        {
            get
            {
                object o = ViewState["ProjectResourceId"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                ViewState["ProjectResourceId"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ProjectResourceId = Convert.ToInt32(Page.Request.QueryString["prid"]);
                if (ProjectResourceId != 0)  // Insert/Add new
                {
                    TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                    var prjres = (from pr in dc.ProjectResources
                                  where pr.ProjectResourceId == ProjectResourceId
                                  select new
                                  {
                                      Client = pr.Project.Client.Name,
                                      Project = pr.Project.Description,
                                      Resource = pr.Resource.Name,
                                      pr.TotalAllocatedHours,
                                      pr.TotalExpectedHours,
                                  }).Single();
                    if (prjres != null)
                    {
                        lblClient.Text = prjres.Client;
                        lblProject.Text = prjres.Project;
                        lblResource.Text = prjres.Resource;
                        lblHoursBilled.Text = prjres.TotalAllocatedHours.ToString();
                        txtExpectedHrs.Text = prjres.TotalExpectedHours.ToString();
                    }
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                ProjectResource projectResource = dc.ProjectResources.Single(pr => pr.ProjectResourceId == ProjectResourceId);
                projectResource.TotalExpectedHours = Convert.ToDecimal(txtExpectedHrs.Text);
                projectResource.LastUpdatedDate = DateTime.Now;
                projectResource.LastUpdatedBy = CommonWFE.CurrentUserName();
                dc.SubmitChanges();
                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ResourceEdit');</script>");
                Context.Response.Flush();
                Context.Response.End();
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                ProjectResource projectResource = dc.ProjectResources.Single(pr => pr.ProjectResourceId == ProjectResourceId);
                dc.ProjectResources.DeleteOnSubmit(projectResource);
                dc.SubmitChanges();
                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ResourceEdit');</script>");
                Context.Response.Flush();
                Context.Response.End();
            }
            catch (Exception ex)
            {

            }
        }
    }
}
